<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/**
	 * CodeIgniter
	 *
	 * An open source application development framework for PHP 4.3.2 or newer
	 *
	 * @package		CodeIgniter
	 * @author		ExpressionEngine Dev Team
	 * @copyright	Copyright (c) 2008 - 2009, EllisLab, Inc.
	 * @license		http://codeigniter.com/user_guide/license.html
	 * @link		http://codeigniter.com
	 * @since		Version 1.0
	 * @filesource
	 */
	// ------------------------------------------------------------------------
	
	/**
	 * @package		CodeIgniter
	 * @subpackage	Libraries
	 * @author		AnhDT
	 */
	require_once APP_LIBRARY_PATH . 'tree/menu/renderer/st_menu_interface_renderer.php'; 
	class st_menu_abstract_renderer implements st_menu_interface_renderer {
		protected $_outputResourcesTime = 0;
		protected $_resources = array(
						'javascript' => array(),
						'css'        => array(),
				  );
		protected $_menu = null;
		
		public function getOutput() {
			if ($this->_outputResourcesTime === 0) {
				if (!function_exists('base_url')) {
					$CI = & get_instance();
					$CI->load->helper('url');
				}
				$_baseUrl = base_url() . 'media/js/';
				$_outSource = '';
				$_css = $this->_resources['css'];
				$_javascript = $this->_resources['javascript'];
				if (count($_css) > 0) {
					foreach ($_css as $_file) {
						$_outSource .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$_baseUrl$_file\"/>\n";	
					}	
				}
				if (count($_javascript) > 0) {
					foreach ($_javascript as $_file) {
						$_outSource .= "<script language=\"javascript\" src=\"$_baseUrl$_file\"></script>\n";	
					}	
				}
				$this->_outputResourcesTime++;
				return $_outSource;
			}
			return '';
		}
		
		/**Duyệt dữ liệu*/
		public function recursiveData(st_menu $_menu) {
			$_output = "";
			if (empty($_menu)) {
				return $_output;	
			}
			if ($_menu->countChildren() == 0) {
				$_output .= "\n<li";
				if ($_menu->isCurrent()) {
					$_output .= " class=\"current\"";	
				}
				$_output .= ">\n";
				$_output .= "<a href=\"".$_menu->getUrl()."\">";
				$_output .= $_menu->getTitle() . "</a>\n</li>";
			} else {
				$_output .= "\n<ul>";
				foreach ($_menu->getChildren() as $_menuItem) {
					$_output .= "\n<li";
					if ($_menuItem->isCurrent()) {
						$_output .= " class=\"current\"";	
					}
					$_output .= ">\n";
					$_output .= "<a href=\"".$_menuItem->getUrl()."\">";
					$_output .= $_menuItem->getTitle() . "</a>";
					if ($_menuItem->countChildren() > 0) {
						$_output .= $this->recursiveData($_menuItem);
					}
					$_output .= "\n</li>";
				}	
				$_output .= "\n</ul>";
			}
			$_classes = implode(' ', $this->getMenu()->getClasses());
			return "\n<ul class=\"$_classes\"" . substr($_output, 2);
		}
		
		/**Lấy về menu*/
		public function & getMenu() {
			$_menu = & $this->_menu;
			return $_menu;
		}
		
		/**Thiết lập menu*/
		public function setMenu(st_menu $_menu) {
			$this->_menu = & $_menu;
			return $this;	
		}
		
		/**Thiết lập file*/
		public function setResources($_resources = array()) {
			if (!is_array($_resources) || count($_resources) === 0) {
				return false;	
			}
			if (!isset($_resources['javascript']) || !isset($_resources['css'])) {
				return false;		
			}
			$this->_resources = $_resources;
			return $this;
		}
	}
?>